package com.lanou.tipsy.adapter.recommend;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.recommend.carousel.CarouselPicEntity;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/21.
 * 推荐-轮播图适配器
 */
public class CarouselAdapter extends PagerAdapter {
    private ArrayList<CarouselPicEntity> datas;
    private Context context;
    private ImageLoader imageLoader;

    public CarouselAdapter(ArrayList<CarouselPicEntity> datas, Context context) {
        this.datas = datas;
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? Integer.MAX_VALUE : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        CarouselPicEntity entity = datas.get(position % datas.size());
        String url = entity.getRandpic();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,
                R.mipmap.logo_2, R.mipmap.error);
        imageLoader.get(url, listener);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
    }
}
